package trillion.burst.bomb;

import trillion.burst.bomb.R;
import android.widget.LinearLayout;
import trillion.burst.bomb.list.BombListActivity;
import trillion.burst.bomb.multi.Bluetooth_IntroActivity;
import trillion.burst.bomb.multi.Bluetooth_sensorActivity;
import trillion.burst.bomb.single.GameOverActivity;
import trillion.burst.bomb.single.SingleActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.*;
import android.view.View;
import com.google.ads.*;

public class TrillionActivity extends Activity implements View.OnClickListener {

	private Button b1, b2, b3, b4;
	private Intent i, i2, i3, i4;
	private Intent broadCastIntent;

	private static MediaPlayer mp;

	private static final String MY_AD_UNIT_ID = "a14f54585eebe7b";
	private AdView adView;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);        
        
		adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);
        LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
        layout.addView(adView);
        adView.loadAd(new AdRequest());

		mp = MediaPlayer.create(this, R.raw.bgm);
		mp.setLooping(true);
		mp.start();
		
		
		// Broadcast
		broadCastIntent = new Intent("trillion.broadcast.finish");
		sendBroadcast(broadCastIntent);

		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		b1 = (Button) findViewById(R.id.bt1);
		b2 = (Button) findViewById(R.id.bt2);
		b3 = (Button) findViewById(R.id.bt3);
		b4 = (Button) findViewById(R.id.bt4);

		b1.setOnClickListener(this);
		b2.setOnClickListener(this);
		b3.setOnClickListener(this);
		b4.setOnClickListener(this);

		i = new Intent(this, SingleActivity.class);
		i2 = new Intent(this, Bluetooth_IntroActivity.class);
		i3 = new Intent(this, BombListActivity.class);
		i4 = new Intent(this, HowTo.class);

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bt1:
			startActivity(i);
			break;
		case R.id.bt2:
			startActivity(i2);
			break;
		case R.id.bt3:
			startActivity(i3);
			break;
		case R.id.bt4:
			startActivity(i4);
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keycode, KeyEvent event) {
		if (keycode == event.KEYCODE_BACK) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(getString(R.string.exit));
			builder.setMessage(getString(R.string.exitconfirm));

			DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if (which == DialogInterface.BUTTON_POSITIVE) {
						finish();
					}
				}
			};
			builder.setPositiveButton(getString(R.string.yes), listener);
			builder.setNegativeButton(getString(R.string.no), listener);
			builder.show();

		}
		return false;
	}

	public void onPause() {
		super.onPause();
		mp.stop();
		mp.release();
		finish();
	}
	public void onDestroy(){
		super.onDestroy();
		adView.destroy();
	}
}